package com.xc.helloworld;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @Package com.xc.helloworld
 * @Description 客户端handler业务逻辑
 * @Author kunshan
 * @Date 2021/1/26 4:58 下午
 */
@ChannelHandler.Sharable   //为了线程安全
public class HandlerClientHello extends SimpleChannelInboundHandler<String> {

    /**
     * 处理接受到的消息
     * @param channelHandlerContext
     * @param msg
     */
    @Override
    public void channelRead0(ChannelHandlerContext channelHandlerContext,String msg){
        System.out.println("处理接受到的消息:"+msg);
    }

    //
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("正在连接... ");
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("连接关闭! ");
        super.channelInactive(ctx);
    }
    /**
     * 处理IO异常事件
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause) throws Exception{
        cause.printStackTrace();
        ctx.close();
    }

}
